﻿using AutoMapper;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace GGGK.Film.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MenusController : ControllerBase
    {
        private readonly IMenuInfoService svc;
        private readonly IMapper mapper;

        public MenusController(IMenuInfoService svc, IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<MenuInfoDto> Query(string menuName="")
        {
            var data = svc.MenuReduct(menuName);
            //买你大巴/
            return new PagedResult<MenuInfoDto>() { code=1,msg="成功",data=data};
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            return new Result() { code=1,msg="成功",data = entity};
        }

        /// <summary>
        /// 获取数据树
        /// </summary>
        /// <returns></returns>
        [HttpGet("getTree")]
        public Result<IEnumerable<MenuInfoDto>> GetTree()
        {
            var menus = svc.Query(o => o.Status == 1 && o.IsShow == 1);
            var parentMenu = menus.Where(o => o.PId == 0).ToList();
            var childMenu = menus.Where(o => o.PId != 0).ToList();

            var menuTree = new List<MenuInfoDto>();

            foreach (var parent in parentMenu)
            {
                menuTree.Add(new MenuInfoDto()
                {
                    id = parent.Id,
                    menuName = parent.MenuName,
                    menuIcon = parent.MenuIcon ?? "",
                    menuUrl = parent.MenuUrl,
                    isShow = parent.IsShow,
                    pId = parent.PId,
                    menuOrder = parent.MenuOrder,
                    status = parent.Status,
                    children = childMenu.Where(c => c.PId == parent.Id).Select(c => new MenuInfoDto()
                    {
                        id = c.Id,
                        menuName = c.MenuName,
                        menuIcon = c.MenuIcon ?? "",
                        menuUrl = c.MenuUrl,
                        isShow=c.IsShow,
                        pId = c.PId,
                        menuOrder = c.MenuOrder,
                        status = c.Status,
                    })
                });
            }

            return new Result<IEnumerable<MenuInfoDto>>() { code=1,msg="成功",data=menuTree};
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(MenuPostDto dto)
        {
            var entity = mapper.Map<Base_MenuInfo>(dto);


            if (svc.Add(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id,MenuPostDto dto)
        {
            var entity = svc.Get(o=>o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            mapper.Map(dto,entity);

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            entity.Status = 0;

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }
    }
}
